
#include <QLabel>
#include <QAction>
#include <QMenuBar>
#include <QStatusBar>
#include <QMessageBox>
#include <QKeySequence>
#include <QContextMenuEvent>

#include "MenuAndToolBar.h"

MenuAndToolBar::MenuAndToolBar(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);

	// create Actions for Menus
	createActions();

	// create Menus
	createMenus();

	// create Tool Bar
	createToolBar();

	// create Status Bar
	createStatusBar();

	// set init status
	showGridAction->setChecked(true);
	showLineNumAction->setChecked(false);
}

MenuAndToolBar::~MenuAndToolBar()
{

}


// create Actions for Menus
void MenuAndToolBar::createActions()
{
	newAction = new QAction(tr("&New"), this);
	newAction->setShortcut(QKeySequence::New);
	newAction->setStatusTip(tr("Create a new file."));
	connect(newAction, SIGNAL(triggered()),
		this, SLOT(newFile()));
	openAction = new QAction(tr("&Open"), this);
	openAction->setShortcut(QKeySequence::Open);
	saveAction = new QAction(tr("&Save"), this);
	saveAction->setShortcut(QKeySequence::Save);
	saveAsAction = new QAction(tr("&SaveAs..."), this);
	saveAsAction->setShortcut(QKeySequence::SaveAs);
	exitAction = new QAction(tr("E&xit"), this);
	exitAction->setShortcut(tr("Ctrl+X"));
	connect(exitAction, SIGNAL(triggered()),
		this, SLOT(close()));

	cutAction = new QAction(tr("Cu&t"), this);
	cutAction->setShortcut(QKeySequence::Cut);
	cutAction->setStatusTip(tr("Cut operation."));
	connect(cutAction, SIGNAL(triggered()),
		this, SLOT(cut()));
	copyAction = new QAction(tr("&Copy"), this);
	copyAction->setShortcut(QKeySequence::Copy);
	copyAction->setStatusTip(tr("Copy operation."));
	connect(copyAction, SIGNAL(triggered()),
		this, SLOT(copy()));
	pasteAction = new QAction(tr("&Paste"), this);
	pasteAction->setShortcut(QKeySequence::Paste);
	pasteAction->setStatusTip(tr("Paste operation."));
	connect(pasteAction, SIGNAL(triggered()),
		this, SLOT(paste()));
	deleteAction = new QAction(tr("&Delete"), this);
	deleteAction->setShortcut(QKeySequence::Delete);

	selectRowAction = new QAction(tr("&Select Row"), this);
	selectColAction = new QAction(tr("&Select Column"), this);
	selectAllAction = new QAction(tr("&Select All"), this);
	selectAllAction->setShortcut(QKeySequence::SelectAll);

	showGridAction = new QAction(tr("Show Grid"), this);
	showGridAction->setCheckable(true);
	showLineNumAction = new QAction(tr("Show Line Number"), this);
	showLineNumAction->setCheckable(true);

	aboutAction = new QAction(tr("&About"), this);
	aboutQtAction = new QAction(tr("About &Qt"), this);
	connect(aboutQtAction, SIGNAL(triggered()),
		qApp, SLOT(aboutQt()));
}

// create Menus
void MenuAndToolBar::createMenus()
{
	// File Menu
	fileMenu = menuBar()->addMenu(tr("&File"));
	fileMenu->addAction(newAction);
	fileMenu->addAction(openAction);
	fileMenu->addAction(saveAction);
	fileMenu->addAction(saveAsAction);
	recentFilesSeperator = fileMenu->addSeparator();
	fileMenu->addAction(exitAction);

	// Edit Menu
	editMenu = menuBar()->addMenu(tr("&Edit"));
	editMenu->addAction(cutAction);
	editMenu->addAction(copyAction);
	editMenu->addAction(pasteAction);
	editMenu->addAction(deleteAction);
	selectSubMenu = editMenu->addMenu(tr("&Select"));
	selectSubMenu->addAction(selectRowAction);
	selectSubMenu->addAction(selectColAction);
	selectSubMenu->addAction(selectAllAction);

	// Option Menu
	optionsMenu = menuBar()->addMenu(tr("&Options"));
	optionsMenu->addAction(showGridAction);
	optionsMenu->addAction(showLineNumAction);

	// Help Menu
	helpMenu = menuBar()->addMenu(tr("&Help"));
	helpMenu->addAction(aboutAction);
	helpMenu->addAction(aboutQtAction);
}

// create Tool Bar
void MenuAndToolBar::createToolBar()
{
	fileToolBar = addToolBar(tr("&File"));
	fileToolBar->addAction(newAction);

	editToolBar = addToolBar(tr("&Edit"));
	editToolBar->addAction(cutAction);
	editToolBar->addAction(copyAction);
	editToolBar->addAction(pasteAction);
}

// create Status Bar
void MenuAndToolBar::createStatusBar()
{
	showGridLabel = new QLabel(tr("Show Grid: "));
	showGridLabel->setIndent(3);
	showGridLabel->setAlignment(Qt::AlignHCenter);
	showGridLabel->setMinimumWidth(showGridLabel->sizeHint().width() + 50);
	showGridLabel->setMinimumHeight(showGridLabel->sizeHint().height());
	connect(showGridAction, SIGNAL(toggled(bool)),
		this, SLOT(updateStatusBar()));

	showLineNumLabel = new QLabel(tr("Show Line Num: "));
	showLineNumLabel->setIndent(3);
	showLineNumLabel->setAlignment(Qt::AlignHCenter);
	showLineNumLabel->setMinimumWidth(showLineNumLabel->sizeHint().width() + 55);
	showLineNumLabel->setMinimumHeight(showLineNumLabel->sizeHint().height());
	connect(showLineNumAction, SIGNAL(toggled(bool)),
		this, SLOT(updateStatusBar()));

	statusBar()->addPermanentWidget(showGridLabel, false);
	statusBar()->addPermanentWidget(showLineNumLabel, false);
}

// create Context Menu
void MenuAndToolBar::contextMenuEvent(QContextMenuEvent *event)
{
	QMenu contextMenu(this);
	contextMenu.addAction(cutAction);
	contextMenu.addAction(copyAction);
	contextMenu.addAction(pasteAction);
	contextMenu.exec(event->globalPos());
}

void MenuAndToolBar::newFile()
{
	QMessageBox::information(this, tr("New File"), tr("Create a new File."));
}

void MenuAndToolBar::cut()
{
	QMessageBox::information(this, tr("Cut"), tr("Cut operation."));
}

void MenuAndToolBar::copy()
{
	QMessageBox::information(this, tr("Copy"), tr("Copy operation."));
}

void MenuAndToolBar::paste()
{
	QMessageBox::information(this, tr("Paste"), tr("Paste operation."));
}

void MenuAndToolBar::updateStatusBar()
{
	QString status;

	status = tr("Show Grid: ");
	status += showGridAction->isChecked() ? tr("true") : tr("false");
	showGridLabel->setText(status);

	status = tr("Show Line Num: ");
	status += showLineNumAction->isChecked() ? tr("true") : tr("false");
	showLineNumLabel->setText(status);
}